import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import WebGL = Laya.WebGL;
import Tween = Laya.Tween;
import Image = Laya.Image
import { Data } from '../Data/data'
import Common from '../Common/Common';
// 转动的圆圈类页面逻辑

export default class Circle extends Laya.Sprite {

    public circle_pro;   //环
    public ring
    public star



    constructor() {
        super()
        Laya.stage.once("clearStarRotate",this,this.clearStarRotate)
        Laya.stage.once("clearRingRotate",this,this.clearRingRotate)
    }

    /**
     * 封装一个生成环的方法
     * @param color 
     * @param startAngle 
     * @param endAngle 
     */
    public circle(startX: number, startY: number, r: number, startAngle: number = 35, endAngle: number = 325) {
        // this.ring  = new Sprite();
        // this.ring.loadImage("images/ring.png")
        // Laya.stage.addChild(this.ring)
        // this.ring.x = startX
        // this.ring.y = startY
        this.ring = new Image();
        this.ring.skin = "images/ring.png"
        this.ring.width = r * 2
        this.ring.height = r * 2
        // this.ring.pivotX = r
        // this.ring.pivotY = r
        this.ring.pivot(r, r).pos(startX, startY);
        // let texture = Laya.loader.getRes("images/ring.png");
        // texture.width = r*2
        // texture.height = r*2
        // texture.pivotX = r
        // texture.pivotY = r
        // texture.pivot(r,r)
        // this.ring.width = r*2
        // this.ring.height = r*2
        // this.ring.size(r*2,r*2);
        // this.ring.graphics.drawTexture(texture);
        Laya.stage.addChild(this.ring);
        Laya.timer.frameLoop(1, this, this.ringRotate)
        var pieMask = new Sprite()
        pieMask.graphics.drawPie(0, 0, r - 10, startAngle, endAngle, '#ffffff');
        pieMask.pos(r, r)
        pieMask.rotation -= 90
        this.ring.mask = pieMask
        // this.ring.addChild(pieMask)
        console.log(this.ring)
        Common.timeArray.push(this);
        return this.ring
    }

    /**
     * 随机陨石
     */
    randomMeteorite():number {
        let random = Math.random()
        let type = random <= 0.5 ? 0 : 1
        return type
    }

    ringRotate(){
        this.ring.rotation += 1
    }

    starRotate(){
        this.star.rotation += 1
    }

    clearStarRotate(){
        Laya.timer.clear(this,this.starRotate)
    }

    clearRingRotate(){
        Laya.timer.clear(this,this.ringRotate)
    }

    /**
     * 星球
     */
    public drawMidCircle(midCircleX: number, midCircleY: number, r: number) {
        this.star = new Laya.Sprite();
        if (Data.GameData.meteoriteNum < 5) {
            let type = this.randomMeteorite()
            if(type == 0){
                var starImg = "images/meteorite.png"
            }else{
                var starImg = "images/meteoriteMini.png"
            }
            Data.GameData.meteoriteNum += 1
        } else {
            var starImg = "images/star" + Data.GameData.starNum + ".png"
            Data.GameData.meteoriteNum = 0
            if (Data.GameData.starNum < 8) {
                Data.GameData.starNum += 1
            } else if (Data.GameData.starNum > 7) {
                Data.GameData.starNum = 8
            }
        }
        this.star.loadImage(starImg)
        if(starImg != "images/meteoriteMini.png"){
            Common.transferImageToCircle(this.star)
        }
        Laya.stage.addChild(this.star)
        this.star.x = midCircleX
        this.star.y = midCircleY
        this.star.width = r * 2
        this.star.height = r * 2
        this.star.pivotX = r
        this.star.pivotY = r
        Laya.timer.frameLoop(1, this, this.starRotate)
        Common.timeArray.push(this);
        // var midCircle: Sprite = new Laya.Sprite();
        // Laya.stage.addChild(midCircle);
        // midCircle.graphics.drawCircle(midCircleX, midCircleY, 70, "#fff");
        // midCircle.zOrder = 3
        return this.star
    }



}